//
//  StatusViewModel.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/29.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import Foundation

class StatusViewModel {
    
    var status: Status
    var memberIcon:UIImage?
    var vipIcon:UIImage?
    
    var retweetStr:String?
    var commentStr:String?
    var likeStr:String?
    
    var statusAttrText:NSAttributedString?
    var retweetedAttrText:NSAttributedString?
    
    var pictureViewSize = CGSize()
    
    var rowHeigth:CGFloat = 0
    
//    var sourceStr:String?
    
    var picURLs: [StatusPicture]? {
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    
    var description: String{
        return status.description
    }
    
    
    init(model:Status) {
        self.status = model
        
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            memberIcon = UIImage(named: imageName)
        }
        
        switch model.user?.verified_type ?? -1{
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        
        default:
            break
        }
        
        retweetStr = countString(count:model.reposts_count , defaultStr: "转发")
        commentStr = countString(count:model.comments_count , defaultStr: "评论")
        likeStr = countString(count:model.attitudes_count , defaultStr: "赞")
        pictureViewSize = calcPictureViewSize(count: (picURLs?.count)!)
        
        let rText = "@" + (status.retweeted_status?.user?.screen_name ?? "")
        + ":"
        + (status.retweeted_status?.text ?? "")
        
        let originalFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        statusAttrText = EmoticonManager.shared.emoticonString(string: model.text ?? "", font:originalFont)
        retweetedAttrText = EmoticonManager.shared.emoticonString(string: rText, font:retweetedFont)
        
        updateRowHeight()
        
//        sourceStr = "来自" + (model.source?.vv_href()?.text ?? "")
    }
    
    func updateRowHeight() {
        
        let margin:CGFloat = 12
        let iconHeight:CGFloat = 34
        let toolBarHeight:CGFloat = 35
        
        var height:CGFloat = 0
        let viewSize = CGSize(width: UIScreen.cz_screenWidth() - 2*margin, height: CGFloat(MAXFLOAT))
//        let originalFont = UIFont.systemFont(ofSize: 15)
//        let retweetedFont = UIFont.systemFont(ofSize: 14)
        height = 2*margin + iconHeight + margin
        
        if let text =  statusAttrText{
            
            height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
            
//            height += (text as NSString).boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: originalFont], context: nil).height
        }
        if status.retweeted_status != nil {
            height += 2*margin
            
            if let text = retweetedAttrText {
                height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
//                height += (text as NSString).boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: retweetedFont], context: nil).height
            }
        }
        
        height += pictureViewSize.height
        height += margin
        height += toolBarHeight
        rowHeigth = height - 4
    }
    
    func updateSingleImageSize(image: UIImage) {
        var size = image.size
        
        let maxWidth:CGFloat = 200
        let minWidth:CGFloat = 40
        
        if size.width > maxWidth {
            size.width = maxWidth
            size.height = size.width * image.size.height / image.size.width
        }
        
        if size.width < minWidth {
            size.width = minWidth
            size.height = size.width * image.size.height / image.size.width / 4
        }
        
        size.height += StatusPictureViewOutterMargin
        pictureViewSize = size
        updateRowHeight()
    }
    
    private func calcPictureViewSize(count:Int?)->CGSize{
        
        if count == 0 || count == nil {
            return CGSize()
        }
        
//        let StatusPictureViewOutterMargin = CGFloat(12)
//        let StatusPictureViewInnerMargin = CGFloat(3)
//        let StatusPictureViewWidth = UIScreen.cz_screenWidth() - 2*StatusPictureViewOutterMargin
//        let StatusPictureItemWidth = (StatusPictureViewWidth - 2*StatusPictureViewInnerMargin)/3
        
        let row = (count! - 1)/3 + 1
        
        let height = StatusPictureViewOutterMargin + CGFloat(row)*StatusPictureItemWidth + CGFloat(row-1) * StatusPictureViewInnerMargin
        
        return CGSize(width: StatusPictureViewWidth, height: height)
    }
    
    private func countString(count:Int64, defaultStr: String)->(String) {
        
        if count == 0 {
            return defaultStr
        }
        
        if count<10000 {
            return count.description
        }
        
        return String(format: "%.02f", Double(count)/10000)
        
    }
    
    
}
